package com.ssy.lingxi.search.controller.mobile;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommodityCollectBatchRequest;
import com.ssy.lingxi.search.api.model.request.CommodityCollectRequest;
import com.ssy.lingxi.search.api.model.response.CollectResponse;
import com.ssy.lingxi.search.api.model.response.CommoditySearchResponse;
import com.ssy.lingxi.search.entity.Commodity;
import com.ssy.lingxi.search.entity.CommodityCollect;
import com.ssy.lingxi.search.entity.CommodityShop;
import com.ssy.lingxi.search.entity.CommoditySoldCount;
import com.ssy.lingxi.search.model.response.CommodityCollectResponse;
import com.ssy.lingxi.search.repository.CommodityRepository;
import com.ssy.lingxi.search.service.ICommodityCollectService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu: APP-商城--商品收藏管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/9/2
 */
@RestController
@RequestMapping("/search/mobile/shop/commodityCollect")
public class MobileCommodityCollectController extends BaseController {

    @Resource
    private ICommodityCollectService commodityCollectService;

    @Resource
    private CommodityRepository commodityRepository;

    /**
     * 查询商品是否已经收藏
     * @param commodityId 商品id
     */
    @RequestMapping(value = "/getCommodityCollect", method = RequestMethod.GET)
    public Wrapper<CollectResponse> getCommodityCollect(@RequestParam Long commodityId) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.getCommodityCollectByCommodityId(commodityId, sysUser, shopId));
    }

    /**
     * 查询商品收藏列表
     * @param pageVO 分页实体
     * @return
     */
    @RequestMapping(value = "/getCommodityCollectList", method = RequestMethod.GET)
    public Wrapper<PageData<CommodityCollectResponse>> getCommodityCollectList(PageVO pageVO) {
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();

        Page<CommodityCollect> result = commodityCollectService.getCommodityCollectList(sysUser, shopId, pageVO);
        List<CommodityCollectResponse> resultList = result.stream().map(commodityCollect -> {
            CommodityCollectResponse commodityCollectResponse = new CommodityCollectResponse();
            commodityCollectResponse.setIsPublish(false);
            Long commodityId = commodityCollect.getCommodityId();
            Long commodityCollectId = commodityCollect.getId();
            Long createTime = commodityCollect.getCreateTime();
            commodityCollectResponse.setId(commodityCollectId);
            commodityCollectResponse.setShopId(commodityCollect.getShopId());
            commodityCollectResponse.setCreateTime(createTime);
            Commodity commodity = commodityRepository.findById(commodityId).orElse(null);
            if(commodity != null){
                CommoditySearchResponse commoditySearchResponse = new CommoditySearchResponse();
                BeanUtil.copyProperties(commodity, commoditySearchResponse);
                commodityCollectResponse.setCommodity(commoditySearchResponse);

                //不同商城显示不同的已售数量
                List<CommoditySoldCount> commoditySoldCountList = commodity.getCommoditySoldCountList();
                if(commoditySoldCountList != null && commoditySoldCountList.size() > 0){
                    commoditySoldCountList.forEach(commoditySoldCount -> {
                        Long commoditySoldCountShopId = commoditySoldCount.getShopId();
                        double sold = commoditySoldCount.getSold();
                        if(commoditySoldCountShopId.equals(shopId)){
                            commoditySearchResponse.setSold(sold);
                        }
                    });
                }

                //判断会员商品是否上架
                List<CommodityShop> commodityShopList = commodity.getCommodityShopList();
                if(commodityShopList != null && commodityShopList.size() > 0){
                    List<Long> shopIdList = commodityShopList.stream().map(CommodityShop::getShopId).collect(Collectors.toList());
                    if(shopIdList.contains(shopId)){
                        commodityCollectResponse.setIsPublish(true);
                    }
                }
            }else{
                return null;
            }

            return commodityCollectResponse;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 新增商品收藏
     * @param commodityCollectRequest 商品收藏实体
     * @return 商品收藏id
     */
    @RequestMapping(value = "/saveCommodityCollect", method = RequestMethod.POST)
    public Wrapper<Long> saveCommodityCollect(@RequestBody CommodityCollectRequest commodityCollectRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.saveCommodityCollect(sysUser, shopId, commodityCollectRequest));
    }

    /**
     * 批量新增商品收藏
     * @param simpleCommodityIdListRequest 商品收藏实体
     * @return 商品收藏id
     */
    @RequestMapping(value = "/saveBatchCommodityCollect", method = RequestMethod.POST)
    public Wrapper<Boolean> saveBatchCommodityCollect(@RequestBody CommodityCollectBatchRequest simpleCommodityIdListRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.saveBatchCommodityCollect(sysUser, shopId, simpleCommodityIdListRequest));
    }

    /**
     * 删除商品收藏
     * @param commodityCollectRequest
     * @return
     */
    @RequestMapping(value = "deleteCommodityCollect", method = RequestMethod.POST)
    public Wrapper<String> deleteCommodityCollect(@RequestBody CommodityCollectRequest commodityCollectRequest){
        Long shopId = this.getHeadersShopId();
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(commodityCollectService.deleteCommodityCollect(sysUser, shopId, commodityCollectRequest));
    }

}
